Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 485)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.89638 12.89965 12.90292 12.90617 12.90939 12.91258 12.91572 12.91881
## [9] 12.92184 12.92479 12.92767 12.93045 12.93313 12.93571 12.93816 12.94049
## [17] 12.94268 12.94473 12.94662 12.94835 12.94990 12.95127 12.95248 12.95356
## [25] 12.95451 12.95534 12.95605 12.95666 12.95716 12.95756 12.95786 12.95808
## [33] 12.95821 12.95826 12.95824 12.95816 12.95801 12.95780 12.95754 12.95723
## [41] 12.95688 12.95649 12.95607 12.95563 12.95517 12.95469 12.95420 12.95370
## [49] 12.95321 12.95272 12.95212 12.95128 12.95021 12.94892 12.94742 12.94571
## [57] 12.94381 12.94172 12.93945 12.93700 12.93440 12.93163 12.92872 12.92567
## [65] 12.92249 12.91918 12.91576 12.91223 12.90860 12.90488 12.90108 12.89720
## [73] 12.89325 12.88925 12.88519 12.88109 12.87696 12.87279 12.86861 12.86442
## [81] 12.86022 12.85603 12.85186 12.84770 12.84357 12.83948 12.83544 12.83145
## [89] 12.82752 12.82366 12.81988 12.81618 12.81258 12.80908 12.80569 12.80242
## [97] 12.79927 12.79626 12.79339 12.79013 12.78598 12.78104 12.77538 12.76908
## [105] 12.76223 12.75490 12.74718 12.73915 12.73089 12.72247 12.71399 12.70553
## [113] 12.69715 12.68896 12.68102 12.67341 12.66623 12.65954 12.65344 12.64800
## [121] 12.64330 12.63943 12.63646 12.63449 12.63357 12.63381 12.63528 12.63807
## [129] 12.64267 12.64938 12.65790 12.66796 12.67927 12.69155 12.70452 12.71789
## [137] 12.73139 12.74473 12.75763 12.76980 12.78098 12.79087 12.80061 12.81149
## [145] 12.82340 12.83626 12.84998 12.86447 12.87963 12.89539 12.91164 12.92830
## [153] 12.94528 12.96249 12.97984 12.99724 13.01459 13.03181 13.04882 13.06551
## [161] 13.08179 13.09759 13.11281 13.12735 13.14114 13.15407 13.16606 13.17702
## [169] 13.18916 13.20452 13.22271 13.24332 13.26598 13.29030 13.31588 13.34234
## [177] 13.36929 13.39634 13.42311 13.44920 13.47422 13.49779 13.51951 13.53901
## [185] 13.55588 13.56974 13.58021 13.58689 13.58940 13.58882 13.58659 13.58282
## [193] 13.57764 13.57115 13.56347 13.55473 13.54503 13.53449 13.52324 13.51137
## [201] 13.49902 13.48630 13.47333 13.46021 13.44708 13.43262 13.41559 13.39614
## [209] 13.37444 13.35066 13.32495 13.29749 13.26844 13.23797 13.20623 13.17340
## [217] 13.13964 13.10511 13.06999 13.03443 12.99860 12.96267 12.92680 12.89115
## [225] 12.85589 12.82119 12.78721 12.75412 12.72207 12.69125 12.66180 12.63220
## [233] 12.60093 12.56821 12.53426 12.49929 12.46352 12.42715 12.39040 12.35349
## [241] 12.31663 12.28004 12.24393 12.20851 12.17399 12.14061 12.10855 12.07805
## [249] 12.04932 12.02256 11.99800 11.97584 11.95631 11.93962 11.92556 11.91371
## [257] 11.90391 11.89602 11.88990 11.88541 11.88239 11.88071 11.88021 11.88077
## [265] 11.88222 11.88443 11.88726 11.89055 11.89416 11.89795 11.90178 11.90550
## [273] 11.90897 11.91203 11.91455 11.91639 11.91739 11.91742 11.91633 11.91397
## [281] 11.91304 11.91591 11.92191 11.93039 11.94070 11.95216 11.96414 11.97596
## [289] 11.98699 11.99655 12.00399 12.00866 12.00989 12.00704 12.00179 11.99627
## [297] 11.99046 11.98435 11.97790 11.97112 11.96397 11.95644 11.94852 11.94019
## [305] 11.93143 11.92223 11.91256 11.90240 11.89039 11.87534 11.85753 11.83726
## [313] 11.81480 11.79044 11.76446 11.73715 11.70880 11.67968 11.65009 11.62031
## [321] 11.59061 11.56130 11.53264 11.50494 11.47846 11.45350 11.43033 11.40926
## [329] 11.39055 11.37449 11.36138 11.34983 11.33831 11.32692 11.31572 11.30479
## [337] 11.29421 11.28404 11.27437 11.26528 11.25683 11.24910 11.24217 11.23612
## [345] 11.23101 11.22693 11.22394 11.22214 11.22158 11.22235 11.22453 11.22818
## [353] 11.23338 11.24021 11.24875 11.25907 11.27124 11.28560 11.30234 11.32131
## [361] 11.34239 11.36541 11.39025 11.41677 11.44482 11.47425 11.50494 11.53674
## [369] 11.56950 11.60309 11.63736 11.67218 11.70740 11.74288 11.77849 11.81407
## [377] 11.84950 11.88462 11.91929 11.95338 11.98675 12.01925 12.05074 12.08108
## [385] 12.11014 12.14102 12.17641 12.21553 12.25757 12.30177 12.34731 12.39342
## [393] 12.43930 12.48416 12.52722 12.56768 12.60475 12.63764 12.66556 12.69161
## [401] 12.71912 12.74770 12.77694 12.80644 12.83579 12.86460 12.89244 12.91893
## [409] 12.94365 12.96621 12.98620 13.00321 13.01684 13.02754 13.03610 13.04265
## [417] 13.04731 13.05019 13.05141 13.05110 13.04937 13.04635 13.04214 13.03688
## [425] 13.03068 13.02367 13.01595 13.00766 12.99890 12.98981 12.98049 12.97107
## [433] 12.96168 12.95242 12.94342 12.93479 12.92594 12.91619 12.90557 12.89410
## [441] 12.88182 12.86874 12.85491 12.84033 12.82504 12.80907 12.79244 12.77518
## [449] 12.75731 12.73887 12.71987 12.70035 12.68033 12.65984 12.63891 12.61756
## [457] 12.59581 12.57370 12.55126 12.52850 12.50545 12.48215 12.45851 12.43443
## [465] 12.40990 12.38492 12.35950 12.33363 12.30730 12.28052 12.25329 12.22559
## [473] 12.19744 12.16882 12.13975 12.11020 12.08019 12.04970 12.01875 11.98732
## [481] 11.95542 11.92303 11.89017 11.85683 11.82300
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 485)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.53738 12.53723 12.53710 12.53701 12.53694 12.53690 12.53689 12.53690
## [9] 12.53694 12.53700 12.53709 12.53720 12.53733 12.53748 12.53766 12.53786
## [17] 12.53808 12.53831 12.53857 12.53885 12.53915 12.53946 12.53978 12.54008
## [25] 12.54038 12.54068 12.54096 12.54125 12.54153 12.54182 12.54210 12.54239
## [33] 12.54268 12.54298 12.54329 12.54360 12.54393 12.54426 12.54462 12.54498
## [41] 12.54537 12.54577 12.54619 12.54663 12.54710 12.54759 12.54811 12.54865
## [49] 12.54923 12.54983 12.55044 12.55102 12.55157 12.55211 12.55263 12.55313
## [57] 12.55362 12.55410 12.55457 12.55504 12.55551 12.55599 12.55647 12.55695
## [65] 12.55745 12.55796 12.55849 12.55904 12.55961 12.56020 12.56083 12.56148
## [73] 12.56217 12.56290 12.56367 12.56448 12.56533 12.56624 12.56720 12.56821
## [81] 12.56927 12.57040 12.57159 12.57285 12.57418 12.57558 12.57705 12.57860
## [89] 12.58023 12.58195 12.58375 12.58564 12.58762 12.58970 12.59187 12.59415
## [97] 12.59653 12.59901 12.60161 12.60340 12.60356 12.60222 12.59954 12.59563
## [105] 12.59065 12.58473 12.57800 12.57062 12.56270 12.55440 12.54585 12.53719
## [113] 12.52856 12.52010 12.51193 12.50421 12.49708 12.49065 12.48509 12.48052
## [121] 12.47709 12.47492 12.47417 12.47496 12.47744 12.48175 12.48845 12.49780
## [129] 12.50942 12.52294 12.53799 12.55420 12.57121 12.58863 12.60611 12.62327
## [137] 12.63974 12.65515 12.66913 12.68131 12.69381 12.70884 12.72616 12.74555
## [145] 12.76678 12.78962 12.81384 12.83920 12.86548 12.89246 12.91989 12.94755
## [153] 12.97521 13.00264 13.02961 13.05590 13.08126 13.10548 13.12831 13.14954
## [161] 13.16893 13.18626 13.20128 13.21625 13.23335 13.25225 13.27266 13.29424
## [169] 13.31669 13.33968 13.36291 13.38606 13.40881 13.43085 13.45186 13.47153
## [177] 13.48954 13.50557 13.51931 13.53045 13.53866 13.54364 13.54621 13.54741
## [185] 13.54730 13.54593 13.54333 13.53955 13.53463 13.52862 13.52157 13.51351
## [193] 13.50449 13.49456 13.48376 13.47214 13.45973 13.44659 13.43275 13.41827
## [201] 13.40318 13.38753 13.37137 13.35474 13.33632 13.31493 13.29082 13.26423
## [209] 13.23543 13.20465 13.17216 13.13819 13.10299 13.06682 13.02993 12.99256
## [217] 12.95497 12.91740 12.88011 12.84334 12.80734 12.77236 12.73866 12.70647
## [225] 12.67606 12.64767 12.61955 12.58990 12.55892 12.52677 12.49365 12.45973
## [233] 12.42520 12.39025 12.35504 12.31976 12.28461 12.24975 12.21537 12.18165
## [241] 12.14878 12.11693 12.08629 12.05705 12.02937 12.00345 11.97947 11.95761
## [249] 11.93805 11.92097 11.90656 11.89500 11.88657 11.88129 11.87891 11.87917
## [257] 11.88184 11.88667 11.89341 11.90182 11.91165 11.92265 11.93458 11.94719
## [265] 11.96024 11.97347 11.98665 11.99953 12.01186 12.02339 12.03388 12.04308
## [273] 12.05075 12.05664 12.06051 12.06572 12.07532 12.08853 12.10460 12.12276
## [281] 12.14225 12.16230 12.18216 12.20105 12.21821 12.23289 12.24431 12.25172
## [289] 12.25435 12.25466 12.25547 12.25657 12.25775 12.25882 12.25955 12.25974
## [297] 12.25919 12.25768 12.25501 12.25096 12.24534 12.23793 12.22852 12.21639
## [305] 12.20113 12.18299 12.16222 12.13906 12.11378 12.08660 12.05780 12.02760
## [313] 11.99628 11.96407 11.93122 11.89799 11.86462 11.83137 11.79848 11.76620
## [321] 11.73479 11.70449 11.67555 11.64823 11.62277 11.59942 11.57843 11.56005
## [329] 11.54453 11.52954 11.51286 11.49488 11.47601 11.45663 11.43716 11.41798
## [337] 11.39950 11.38212 11.36623 11.35224 11.34054 11.33153 11.32562 11.32126
## [345] 11.31678 11.31236 11.30821 11.30451 11.30144 11.29920 11.29797 11.29795
## [353] 11.29932 11.30228 11.30700 11.31369 11.32252 11.33401 11.34842 11.36556
## [361] 11.38525 11.40732 11.43158 11.45785 11.48596 11.51571 11.54695 11.57947
## [369] 11.61310 11.64767 11.68299 11.71887 11.75515 11.79164 11.82816 11.86453
## [377] 11.90057 11.93610 11.97094 12.00490 12.03782 12.06950 12.09977 12.12845
## [385] 12.15535 12.18284 12.21306 12.24550 12.27967 12.31507 12.35120 12.38756
## [393] 12.42364 12.45896 12.49301 12.52530 12.55531 12.58256 12.60654 12.62968
## [401] 12.65447 12.68055 12.70752 12.73503 12.76268 12.79012 12.81695 12.84280
## [409] 12.86729 12.89006 12.91072 12.92889 12.94421 12.95708 12.96825 12.97780
## [417] 12.98581 12.99238 12.99757 13.00149 13.00421 13.00582 13.00641 13.00606
## [425] 13.00486 13.00289 13.00024 12.99700 12.99324 12.98906 12.98453 12.97976
## [433] 12.97481 12.96978 12.96475 12.95981 12.95454 12.94849 12.94167 12.93410
## [441] 12.92581 12.91681 12.90713 12.89679 12.88580 12.87418 12.86196 12.84915
## [449] 12.83578 12.82186 12.80742 12.79248 12.77704 12.76115 12.74481 12.72804
## [457] 12.71087 12.69331 12.67539 12.65713 12.63854 12.61964 12.60037 12.58062
## [465] 12.56039 12.53969 12.51851 12.49685 12.47470 12.45207 12.42896 12.40536
## [473] 12.38127 12.35670 12.33163 12.30606 12.28000 12.25345 12.22640 12.19885
## [481] 12.17079 12.14224 12.11317 12.08361 12.05353
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 485)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.76580 11.77401 11.78220 11.79035 11.79846 11.80651 11.81448 11.82235
## [9] 11.83012 11.83777 11.84528 11.85263 11.85982 11.86682 11.87363 11.88022
## [17] 11.88658 11.89270 11.89856 11.90414 11.90944 11.91443 11.91910 11.92344
## [25] 11.92742 11.93104 11.93428 11.93712 11.93956 11.94161 11.94331 11.94470
## [33] 11.94578 11.94657 11.94709 11.94735 11.94738 11.94717 11.94677 11.94616
## [41] 11.94539 11.94446 11.94338 11.94218 11.94087 11.93947 11.93799 11.93646
## [49] 11.93487 11.93327 11.93165 11.93004 11.92845 11.92690 11.92540 11.92398
## [57] 11.92264 11.92089 11.91825 11.91476 11.91046 11.90538 11.89958 11.89307
## [65] 11.88592 11.87815 11.86981 11.86093 11.85157 11.84174 11.83151 11.82090
## [73] 11.80995 11.79871 11.78722 11.77551 11.76363 11.75161 11.73950 11.72733
## [81] 11.71515 11.70299 11.69090 11.67891 11.66707 11.65540 11.64397 11.63279
## [89] 11.62193 11.61140 11.60126 11.59154 11.58228 11.57353 11.56532 11.55769
## [97] 11.55069 11.54434 11.53870 11.53381 11.52969 11.52640 11.52397 11.52244
## [105] 11.52185 11.52224 11.52304 11.52370 11.52428 11.52486 11.52551 11.52631
## [113] 11.52732 11.52862 11.53028 11.53236 11.53495 11.53812 11.54193 11.54646
## [121] 11.55178 11.55796 11.56508 11.57320 11.58240 11.59275 11.60432 11.61719
## [129] 11.63342 11.65455 11.67984 11.70856 11.73999 11.77339 11.80803 11.84317
## [137] 11.87810 11.91206 11.94435 11.97421 12.00092 12.02375 12.04547 12.06920
## [145] 12.09476 12.12198 12.15068 12.18068 12.21181 12.24388 12.27671 12.31014
## [153] 12.34397 12.37804 12.41216 12.44616 12.47985 12.51307 12.54562 12.57734
## [161] 12.60804 12.63755 12.66569 12.69228 12.71714 12.74009 12.76097 12.77957
## [169] 12.79731 12.81561 12.83432 12.85332 12.87248 12.89165 12.91072 12.92954
## [177] 12.94797 12.96590 12.98318 12.99969 13.01528 13.02983 13.04319 13.05525
## [185] 13.06586 13.07490 13.08222 13.08770 13.09120 13.09337 13.09488 13.09570
## [193] 13.09577 13.09503 13.09343 13.09092 13.08745 13.08296 13.07740 13.07071
## [201] 13.06284 13.05375 13.04337 13.03165 13.01855 13.00303 12.98426 12.96247
## [209] 12.93788 12.91070 12.88117 12.84950 12.81592 12.78065 12.74391 12.70592
## [217] 12.66691 12.62710 12.58671 12.54596 12.50508 12.46429 12.42381 12.38386
## [225] 12.34467 12.30646 12.26945 12.23387 12.19993 12.16785 12.13787 12.10777
## [233] 12.07537 12.04095 12.00481 11.96724 11.92851 11.88892 11.84875 11.80829
## [241] 11.76782 11.72764 11.68802 11.64926 11.61164 11.57545 11.54097 11.50850
## [249] 11.47831 11.45070 11.42595 11.40435 11.38618 11.37173 11.36125 11.35460
## [257] 11.35150 11.35168 11.35485 11.36075 11.36909 11.37959 11.39198 11.40599
## [265] 11.42132 11.43771 11.45488 11.47255 11.49043 11.50827 11.52577 11.54266
## [273] 11.55866 11.57350 11.58689 11.59857 11.60824 11.61564 11.62049 11.62250
## [281] 11.62542 11.63262 11.64330 11.65666 11.67191 11.68823 11.70483 11.72092
## [289] 11.73568 11.74831 11.75802 11.76401 11.76547 11.76161 11.75140 11.73496
## [297] 11.71325 11.68717 11.65768 11.62569 11.59213 11.55795 11.52406 11.49141
## [305] 11.46091 11.43351 11.41013 11.39170 11.37477 11.35544 11.33399 11.31068
## [313] 11.28580 11.25961 11.23239 11.20440 11.17593 11.14724 11.11861 11.09032
## [321] 11.06262 11.03581 11.01014 10.98589 10.96335 10.94277 10.92443 10.90860
## [329] 10.89557 10.88559 10.87895 10.87481 10.87214 10.87088 10.87098 10.87237
## [337] 10.87501 10.87883 10.88379 10.88982 10.89688 10.90491 10.91384 10.92363
## [345] 10.93423 10.94557 10.95760 10.97026 10.98351 10.99728 11.01152 11.02617
## [353] 11.04118 11.05650 11.07206 11.08781 11.10371 11.12046 11.13875 11.15850
## [361] 11.17959 11.20192 11.22540 11.24993 11.27541 11.30172 11.32879 11.35650
## [369] 11.38475 11.41345 11.44249 11.47178 11.50121 11.53068 11.56010 11.58936
## [377] 11.61836 11.64701 11.67519 11.70282 11.72979 11.75601 11.78136 11.80576
## [385] 11.82909 11.85282 11.87824 11.90504 11.93291 11.96153 11.99059 12.01977
## [393] 12.04876 12.07725 12.10492 12.13146 12.15655 12.17988 12.20114 12.22259
## [401] 12.24638 12.27204 12.29908 12.32703 12.35540 12.38371 12.41149 12.43825
## [409] 12.46351 12.48679 12.50762 12.52551 12.53997 12.55179 12.56208 12.57092
## [417] 12.57840 12.58458 12.58954 12.59336 12.59610 12.59786 12.59870 12.59869
## [425] 12.59792 12.59646 12.59438 12.59176 12.58867 12.58520 12.58141 12.57738
## [433] 12.57319 12.56891 12.56462 12.56039 12.55583 12.55051 12.54446 12.53770
## [441] 12.53025 12.52215 12.51341 12.50407 12.49415 12.48368 12.47267 12.46117
## [449] 12.44918 12.43675 12.42389 12.41063 12.39700 12.38301 12.36871 12.35411
## [457] 12.33924 12.32412 12.30878 12.29325 12.27755 12.26171 12.24563 12.22920
## [465] 12.21243 12.19531 12.17784 12.16001 12.14182 12.12328 12.10438 12.08511
## [473] 12.06549 12.04549 12.02513 12.00439 11.98329 11.96180 11.93995 11.91771
## [481] 11.89509 11.87209 11.84871 11.82494 11.80078
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")